<template>
	<xn-form-container
		:title="formData.id ? '编辑订单' : '增加订单'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="用户名称：" name="user_id">
				<a-select
					v-model:value="formData.user_id"
					show-search
					placeholder="请选择客户"
					:options="userOptions"
					allow-clear
					showSearch
					option-filter-prop="label"
				/>
			</a-form-item>
			<a-form-item label="服务模块：" name="service_module_id">
				<a-select
					v-model:value="formData.service_module_id"
					placeholder="请选择服务模块"
					:options="moduleOptions"
					allow-clear
					showSearch
					option-filter-prop="label"
				/>
			</a-form-item>
			<a-form-item label="时间范围：" name="time">
				<a-range-picker
					v-model:value="formData.time"
					:show-time="{
						hideDisabledOptions: true,
						defaultValue: [dayjs('00:00:00', 'HH:mm:ss'), dayjs('23:59:59', 'HH:mm:ss')]
					}"
					format="YYYY-MM-DD HH:mm:ss"
				/>
			</a-form-item>
			<a-form-item label="状态：" name="status">
				<a-select
					v-model:value="formData.status"
					placeholder="请选择状态"
					:options="[
						{ label: '创建中', value: 1 },
						{ label: '生效', value: 2 },
						{ label: '过期', value: 3 }
					]"
					allow-clear
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button class="xn-mr8" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required, rules } from '@/utils/formRules'
	import smCrypto from '@/utils/smCrypto'
	import dayjs from 'dayjs'
	import api from './api'
	import userApi from '../../customerInfo/api'
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({
		time: []
	})

	const columns = [
		{
			title: '用户名称',
			dataIndex: 'username',
			width: '200px',
			fixed: 'left',
			ellipsis: { showTitle: true }
		},
		{
			title: '服务模块',
			dataIndex: 'name',
			width: 200
		},
		{
			title: '开始时间',
			dataIndex: 'start_time',
			width: 200
		},
		{
			title: '结束时间',
			dataIndex: 'end_time',
			width: 200
		},
		{
			title: '状态',
			dataIndex: 'status',
			width: 100
		},
		{
			title: '创建时间',
			dataIndex: 'create_time',
			width: 200
		},
		{
			title: '修改时间',
			dataIndex: 'modified_time',
			width: 200
		},
		{
			title: '操作',
			dataIndex: 'action',
			fixed: 'right',
			align: 'center',
			width: '200px'
		}
	]
	let index = 0
	// 打开抽屉
	const onOpen = (record, id) => {
		visible.value = true
		index = id
		formData.value = {
			sortCode: 99
		}
		if (record) {
			formData.value = Object.assign({}, record)
			formData.value.time = [dayjs(record.start_time), dayjs(record.end_time)]
		} else {
			formData.value = {}
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}
	// 图标选择器回调
	const iconCallBack = (value) => {
		if (value) {
			formRef.value.clearValidate('icon')
		}
		formData.value.icon = value
	}
	const moduleOptions = [
		{
			label: '基础版本实时人口投射',
			value: 1
		},
		{
			label: '高精版本实时人口投射',
			value: 2
		},
		{
			label: '人口投射分析',
			value: 3
		},
		{
			label: '全域人口投射分析',
			value: 4
		},
		{
			label: 'app可视化行为',
			value: 5
		},
		{
			label: '人口迁徙分析',
			value: 6
		},
		{
			label: '旅游行为分析',
			value: 7
		},
		{
			label: '全域人口投射系统',
			value: 8
		},
		{
			label: '互联网数据列表',
			value: 9
		},
		{
			label: '互联网数据系统',
			value: 10
		},
		{
			label: '大数据满意度洞察',
			value: 11
		}
	]

	const userOptions = ref([])

	const loadUserList = () => {
		userApi.modulePage({ pageNow: 1, pagesize: 10000 }).then((res) => {
			userOptions.value = (res.record || []).map((item) => {
				return {
					label: item.username,
					value: item.id
				}
			})
		})
	}
	loadUserList()

	// 默认要校验的
	const formRules = {
		user_id: [required('请输入客户名称')],
		service_module_id: [required('请输入服务模块')],
		time: [required('请选择时间范围')],
		status: [required('请选择状态')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				let user_name = userOptions.value.find((item) => {
					return item.value == formData.value.user_id
				}).label
				const name = moduleOptions.find((item) => item.value == formData.value.service_module_id).label
				api
					.submitForm(
						{
							...formData.value,
							...(formData.value.id
								? {
										create_time: dayjs(formData.value.create_time).format('YYYY-MM-DD HH:mm:ss')
								  }
								: {
										create_time: dayjs().format('YYYY-MM-DD HH:mm:ss')
								  }),
							modified_time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
							username: user_name,
							start_time: dayjs(formData.value.time[0]).format('YYYY-MM-DD HH:mm:ss'),
							end_time: dayjs(formData.value.time[1]).format('YYYY-MM-DD HH:mm:ss'),
							name: name
						},
						formData.value.id
					)
					.then(() => {
						onClose()
						emit('successful')
					})
			})
			.catch((error) => {
				console.log(error)
			})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
